package com.timoon.webcomponent.utility.product.girdpic;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.GridView;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.timoon.component.panel.common.DefaultEmptyPanel;
import com.timoon.sample.dao.model.Product;
import com.timoon.webcomponent.utility.product.linepic.component.SingleSmallPicProductPanel;

/**
 *<p>宫格图片</p>
 *<p>300px,3*3</p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public class GridBigPicPanel extends Panel {
	
	@SpringBean(name="com.timoon.sample.service.GridBigPicServiceImpl")
	private GridBigPicService service;
	
	private Logger logger=LoggerFactory.getLogger(this.getClass());

	private Integer type;
	
	private String title;
	
	private Class<? extends WebPage> targetPage;
	
	private int defaultCols=4;
	
	private List<Product> products;
	
	/**
	 * 
	 * @param id
	 * @param title
	 * @param type 筛选商品的type，与List<Product> products比较具有优先权
	 */
	public GridBigPicPanel(String id,String title,Integer type,Class<? extends WebPage> targetPage) {
		super(id);
		// TODO Auto-generated constructor stub
		
		this.targetPage=targetPage;
		
		this.type=type;
		
		this.title=title;
		
		renderPanel();
	}
	
	/**
	 * 此方法不适用type筛选商品，而是直接传递需要显示的商品
	 * @param id
	 * @param title
	 * @param products 需要显示的商品
	 * @param targetPage
	 */
	public GridBigPicPanel(String id,String title,List<Product> products,Class<? extends WebPage> targetPage) {
		// TODO Auto-generated constructor stub
		super(id);
		
		this.targetPage=targetPage;
		
		this.title=title;
		
		this.products=products;
		
		renderPanel();
	}
	
	/**
	 * 
	 * @param id
	 * @param title
	 * @param type
	 * @param cols 列
	 * @param service
	 * @param targetPage
	 */
	public GridBigPicPanel(String id,String title,Integer type,int cols,GridBigPicService service,Class<? extends WebPage> targetPage) {
		super(id);
		
		this.defaultCols=cols;
		
		this.targetPage=targetPage;
		
		this.service=service;
		
		this.type=type;
		
		this.title=title;
		
		renderPanel();
		
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		renderTitle();
		
		renderGrid();
	}

	private void renderTitle() {
		// TODO Auto-generated method stub
		add(new Label("wk_title",title));
	}

	private void renderGrid() {
		// TODO Auto-generated method stub
		
		List<Product> products=null;
		if(type==null){
			products=this.products;
		}else{
			products=this.service.getPruductsByType(type);
		}
		
		if(products!=null&&products.size()>0){
		}else{
			logger.debug("没有找到type="+type+"的商品");
		}
		
		IDataProvider<Product> paths=new ListDataProvider<Product>(products);
		
		GridView<Product> gridView=new GridView<Product>("rows",paths){

			@Override
			protected void populateEmptyItem(Item<Product> arg0) {
				// TODO Auto-generated method stub
				arg0.add(new DefaultEmptyPanel("wk_cell"));
			}

			@Override
			protected void populateItem(Item<Product> arg0) {
				// TODO Auto-generated method stub
				arg0.add(new SingleSmallPicProductPanel("wk_cell",arg0.getModelObject(),targetPage));
			}
			
		};
		add(gridView);
		
		gridView.setColumns(defaultCols);
	}

}
